new Vue({
  el: '#app',
  data() {
    return {
      form: {
        Start_Date: '',
        End_Date: '',
        App_ID: '',
        token: $.cookie("token"),
      },
      total: 0,// 总条数
      radio: '',
      formData: {},
      ranking: [],
      tableData: [//表格数据
        // {
        //   Ranking: 0,   //排名
        //   Trade_Name: 0,   //商品名称
        //   Quantity: 0,  //销量
        //   sales_Volume: 0,  //销售额
        // }
      ],
      member: {},
      store: [],
      // dialogVisible2: false,//分配供应商窗口
      // dialogForm: false,
      loading: false,//节流
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      time: '',

    }
  },
  mounted() {
    this.getStore()
    this.getData(this.form)
  },
  computed: {
    formatTime1() {
      return function (times) {
        if (times) {
          let date = new Date(times);
          let year = date.getFullYear();
          let month = date.getMonth() + 1;
          let day = date.getDate();
          if (month < 10) {
            month = "0" + month;
          }
          if (day < 10) {
            "0" + day
          }
          return `${year}-${month}-${day}`;
        } else { return '' }
      }
    },
    //去.0
    ZeroRemoval() {
      return function (params) {
        params = params.replace('.0', '')
        return params
      }
    },
    //千分位处理
    thousands() {
      return function (params) {
        params = Number(params).toFixed(2)
        return params.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
      }
    },
    //百分比处理
    toPercent() {
      return function (point) {
        point = Number(point * 100).toFixed(2);
        return point += "%";
      }
    },
  },
  methods: {
    getData(data) {
      let _this = this;
      $.ajax({
        type: 'POST',
        url: DomainName + "/getMemberAnalyseAction",
        data,
        success: function (res) {
          // _this.tableData = res.Data.tabledata
          // _this.price = res.Total
          // _this.total = res.Data.limit
          console.log(res);
          // res.member.Collection_Amount_Today_Rights = _this.ZeroRemoval(res.member.Collection_Amount_Today_Rights)
          // res.member.Collection_Amount_Today = _this.ZeroRemoval(res.member.Collection_Amount_Today)
          // res.member.Collection_Amount = _this.ZeroRemoval(res.member.Collection_Amount)
          // res.member.Collection_Amount_Rights = _this.ZeroRemoval(res.member.Collection_Amount_Rights)

          // res.member.Collection_Amount_Rights = parseFloat(res.member.Collection_Amount_Rights)
          console.log(res.member.Collection_Amount_Rights)
          // res.member.Collection_Amount_Today = Number(res.member.Collection_Amount_Today).toFixed(2)
          // res.member.Collection_Amount = Number(res.member.Collection_Amount).toFixed(2)
          // res.member.Collection_Amount_Rights = Number(res.member.Collection_Amount_Rights).toFixed(2)


          res.member.Collection_Amount_Today_Rights = Number(res.member.Collection_Amount_Today_Rights).toFixed(2)
          res.member.Collection_Amount_Today = Number(res.member.Collection_Amount_Today).toFixed(2)
          res.member.Collection_Amount = Number(res.member.Collection_Amount).toFixed(2)
          res.member.Collection_Amount_Rights = Number(res.member.Collection_Amount_Rights).toFixed(2)

          res.ranking_rights.Sales_Amount = Number(res.member.Sales_Amount).toFixed(2)
          console.log(res);

          _this.member = res.member
          _this.tableData = res.ranking_rights
          _this.ranking = res.ranking
          // let data = [
          //   { value: 1048, name: '会员' },
          //   { value: 735, name: '非会员' },
          // ]
          _this.mychart(res.bar)

        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    // 获取门店
    getStore() {
      let _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + "/getStoreAppIDAction",
        data: {
          token: $.cookie("token")
        },
        success: function (res) {
          _this.store = res
          console.log(res)

        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    // 饼图数据
    mychart(data) {
      // 初始化
      var myChart = echarts.init(document.getElementById('ceshi'));
      option = {
        // 大标题
        title: {
          text: '消费占比',
          left: 'center'
        },
        // 悬浮提示
        tooltip: {
          trigger: 'item'
        },
        // 左上角标题
        legend: {
          orient: '1251',
          left: 'left'
        },
        // 饼图颜色
        color: ['rgb(18, 150, 219)', 'rgb(54, 171, 96)'],
        // 配置项
        series: [

          {
            // 类型
            type: 'pie',
            // 悬浮提示字
            name: '销售量',
            // 饼图大小
            radius: '75%',
            // 数据
            data,
            // 延伸标题
            label: {
              // formatter: "{b} : {c} ({d}%)" // b代表名称，c代表对应值，d代表百分比
              formatter: '{b} : {c} ({d}%)'
            }
          },

        ],
      }
      // 调用渲染
      myChart.setOption(option);
    },
    accAdd(arg1, arg2) {	//浮点型相加
      var r1, r2, m;
      try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
      try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
      m = Math.pow(10, Math.max(r1, r2))
      return (arg1 * m + arg2 * m) / m
    },
    //表格展开
    handleChildren(row) {
      console.log(row.Children.article_info);
      _this = this
      $.ajax({
        url: DomainName + '/getReconciliationPriceDetailAction',
        type: 'POST',
        data: {
          Article_SerialNumber: row.Article_SerialNumber,
          Supplier_SerialNumber: row.Supplier_SerialNumber,
          token: $.cookie("token")
        },
        success: function (data) {
          row.Children.article_info = data
          console.log(row.Children.article_info);
        },
        dataType: "json"
      });
      // }

    },
    // 改变日期
    DateChange() {
      if (this.time) {
        this.form.Start_Date = this.time[0]
        this.form.End_Date = this.time[1]
      } else {
        this.form.Start_Date = ''
        this.form.End_Date = ''
      }
      this.radio == '0'
      this.getData(this.form);
    },
    handlechange() {
      if (this.radio == '1') {
        this.form.Start_Date = this.formatTime1(new Date().getTime() - 60 * 60 * 24 * 365 * 1000) + ' 15:00:00',
          this.form.End_Date = this.formatTime1(new Date().getTime()) + ' 15:00:00',
          this.getData(this.form);
      } else if (this.radio == '2') {
        this.form.Start_Date = this.formatTime1(new Date().getTime() - 60 * 60 * 24 * 30 * 1000) + ' 15:00:00',
          this.form.End_Date = this.formatTime1(new Date().getTime()) + ' 15:00:00',
          this.getData(this.form);
      } else if (this.radio == '3') {
        this.form.Start_Date = this.formatTime1(new Date().getTime() - 60 * 60 * 24 * 7000) + ' 15:00:00',
          this.form.End_Date = this.formatTime1(new Date().getTime()) + ' 15:00:00',
          this.getData(this.form);
        console.log(this.form.Start_Date)
      }
    },
    handleclassify() {//分类查询
      this.getData(this.form);
    },
    handleSizeChange(val) {//分页查询
      this.form.page = 1;
      this.form.limit = val;
      this.getData(this.form)
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getData(this.form)
    },
  },
})